package in.muton.panels.paginator.badgehelpers.user;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.roles.DeptRoleCheck;
import in.muton.edushare.components.messaging.api.UserMessage;
import in.muton.edushare.processing.objects.DeptDataHandler;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.UserDeptLinkDAO;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.UserDeptLink;
import com.edushare.common.vo.Users;

public class DeptBadgeRoles extends BadgeRoles {
	private static final long serialVersionUID = -7941248076605267909L;
	public DeptBadgeRoles(IModel<Departments> model) {
		this.deptModel = model;
		roleMap = new HashMap<String, Integer>();
		roleMap.put("Administrator", UserDeptLink.PRIMARY_ADMIN);
		roleMap.put("Faculty", UserDeptLink.TEACHER);
		roleMap.put("Student", UserDeptLink.STUDENT);
	}

	@Override
	public String changeRole(Users user, String role) {
		if(isLastAdmin(user))
			return "Cannot change role for user "+ user.getCompleteUserName()+ 
			"as this user is the last administrator for this department";
		
		String message = "The role of " + user.getCompleteUserName()
			+ " in the department "
			+ deptModel.getObject().getDeptName()
			+ " has been changed to " + role + (getRole(user).equals("")?"":" from "+ getRole(user));
		UserDeptLinkDAO ud = new UserDeptLinkDAO(EduShareSession.get().getSessionORMInterface());
		ud.changeRole(deptModel.getObject(), user, roleMap.get(role));
		
		CayenneList<Users> userList = new CayenneList<Users>(null);
		userList.add(user);
		userList.add(EduShareSession.get().getLoggedInUser());
		userList.add(new DeptDataHandler(deptModel).getPrimaryAdmins());
		new UserMessage(message).setReciever(userList).setReciever(
				deptModel.getObject()).setPrivacy(Messages.PRIVACY_TYPE_PUBLIC)
				.send();
		return "";
	}

	@Override
	public List<String> getOtherRoles(Users user) {
		List<String> rList = new ArrayList<String>();
		int role = DeptRoleCheck.getRole(deptModel.getObject(), user);
		for(String key:roleMap.keySet()){
			if(roleMap.get(key)!=role)
				rList.add(key);
		}
		return rList;
	}

	@Override
	public String removeUser(Users user) {
		DeptRoleCheck drc = new DeptRoleCheck(deptModel.getObject(), user);
		if(new DeptDataHandler(deptModel).getPrimaryAdmins().getObject().size() == 1 &&
				drc.isAdmin()){
			return "Cannot remove "+ user.getCompleteUserName()+" as this user is the only administrator for this department";
		}
		
		new UserDeptLinkDAO(EduShareSession.get().getSessionORMInterface())
				.remove(deptModel.getObject(), user);
		
		String message = "The user "+user.getCompleteUserName()+" has been removed from"+
			" the department "+ deptModel.getObject().getDeptName();
		CayenneList<Users> userList = new CayenneList<Users>(null);
		userList.add(user);
		userList.add(EduShareSession.get().getLoggedInUser());
		userList.add(new DeptDataHandler(deptModel).getPrimaryAdmins());
		new UserMessage(message).setReciever(userList).setReciever(
				deptModel.getObject()).setPrivacy(Messages.PRIVACY_TYPE_PUBLIC)
				.send();
		return "";
	}
	
	@Override
	public String getRole(Users user) {
		Integer role = DeptRoleCheck.getRole(deptModel.getObject(), user);
		Iterator<String> it = roleMap.keySet().iterator();
		while(it.hasNext()){
			String s = it.next();
			if(roleMap.get(s).equals(role)){
				return s;
			}
		}
		return "";
	}
	
	@Override
	public boolean isAdmin() {
		Users user = EduShareSession.get().getLoggedInUser();
		if(DeptRoleCheck.isPrimaryAdmin(deptModel.getObject(), user))
			return true;
		if(DeptRoleCheck.isUnivAdmin(deptModel.getObject(), user))
			return true;
		return false;
	}

	private HashMap<String, Integer> roleMap = null;
	private IModel<Departments> deptModel = null;
	
	@Override
	public boolean isInvited(Users user) {
		List<UserDeptLink> udl = user.getDepartmentLinks();
		for(UserDeptLink u:udl){
			if(u.getDepartmentLink() == deptModel.getObject() 
					&& u.getStatus()==UserDeptLink.STATUS_INVITED)
				return true;
		}
		return false;
	}

	@Override
	public boolean isLastAdmin(Users user) {
		if(
				roleMap.get(getRole(user))!=null &&
				roleMap.get(getRole(user)).equals(UserDeptLink.PRIMARY_ADMIN) && 
				new DeptDataHandler(deptModel).getPrimaryAdmins().getObject().size()<2
		)
		{
			return true;
		}
		return false;
	}
}
